/**
 * Copyright (C), 2015-2021, XXX有限公司
 * FileName: XjfInStoreServiceImpl
 * Author:   Administrator
 * Date:     2021/8/13 0013 14:39
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package cn.hxlc.tie.demo.modular.jf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hxlc.tie.demo.core.enums.WorkOrderEnum;
import cn.hxlc.tie.demo.modular.jf.entity.XjfInStore;

import cn.hxlc.tie.demo.modular.jf.entity.XjfWorkOrder;
import cn.hxlc.tie.demo.modular.jf.entity.XjfWorkOrderEntry;
import cn.hxlc.tie.demo.modular.jf.mapper.XjfInStoreMapper;

import cn.hxlc.tie.demo.modular.jf.model.params.XjfInStoreParam;
import cn.hxlc.tie.demo.modular.jf.model.result.XjfInStoreResult;

import cn.hxlc.tie.demo.modular.jf.service.XjfInStoreService;

import cn.hxlc.tie.demo.modular.jf.service.XjfWorkOrderEntryService;
import cn.hxlc.tie.demo.modular.jf.service.XjfWorkOrderService;
import cn.hxlc.tie.libs.mp.page.PageFactory;
import cn.hxlc.tie.model.exp.ServiceException;
import cn.hxlc.tie.model.page.PageResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;

import static cn.hxlc.tie.demo.modular.jf.controller.XjfWorkOrderController.getCurrentUserid;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author Administrator
 * @create 2021/8/13 0013
 * @since 1.0.0
 */
@Service
@Transactional
public class XjfInStoreServiceImpl extends ServiceImpl<XjfInStoreMapper, XjfInStore> implements XjfInStoreService {

    @Autowired
    private XjfWorkOrderEntryService xjfWorkOrderEntryService;

    @Autowired
    private XjfWorkOrderService xjfWorkOrderService;

    @Override
    public PageResult<XjfInStoreResult> findPageBySpec(XjfInStoreParam param){
        Page pageContext = getPageContext();
        IPage<XjfInStoreResult> page = this.baseMapper.customPageList(pageContext, param);
        return new PageResult<>(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(Object[] fid) {
        XjfInStore info ;
        for(int i=0;i<fid.length;i++){
            info = this.getById((Integer)fid[i]);
            if(info.getFstatus()!=null && info.getFstatus().equals(WorkOrderEnum.SH)){
                throw new ServiceException(500,"存在审核状态数据，删除失败！");
            }
            this.removeById((Integer)fid[i]);
            //distDataConsumer.distData("tie-mdm-material", DistTypeConstants.DIST_SAVE,info.getFid());
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override

    public void add(XjfInStoreParam param){

        XjfWorkOrderEntry xjfWorkOrderEntry = xjfWorkOrderEntryService.getById(param.getFworkorderentrtyid());

        XjfWorkOrder xjfWorkOrder = xjfWorkOrderService.getById(xjfWorkOrderEntry.getFparentid());

        if(param.getFworkorderid()==null) param.setFworkorderid(xjfWorkOrder.getFid());

        param.setFcreatorid(getCurrentUserid());

        param.setFbizdate(new Date());

        if(param.getFworkorderid()==null) param.setFworkorderid(xjfWorkOrder.getFid());

        if(xjfWorkOrder.getFstatus().getCode()<2){
            throw new ServiceException(500,"" +
                    "未审核的订单不能进行入库操作");
        }
        else{
            if(xjfWorkOrderEntry.getFinstoretype().getCode()==2){
                throw new ServiceException(500,"" +
                        "此订单入库方式为不入库");
            }
            else{
                param.setFstatus(WorkOrderEnum.ZC);

                param.setFcreatetime(new Date());

                param.setFbizdate(new Date());

                XjfInStore entity = getEntity(param);

                this.save(entity);
            }

        }
    }

    private XjfInStore getEntity(XjfInStoreParam param) {
        XjfInStore entity = new XjfInStore();
        BeanUtil.copyProperties(param, entity);
        return entity;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(XjfInStoreParam param){

        XjfInStore oldEntity = getOldEntity(param);

        XjfInStore newEntity = getEntity(param);

        if(param.getFwarehouseid()==null){
            throw new ServiceException(500,"仓库不能为空");
        }

        newEntity.setFstatus(WorkOrderEnum.SH);

        XjfWorkOrderEntry xjfWorkOrderEntry = xjfWorkOrderEntryService.getById(param.getFworkorderentrtyid());

        xjfWorkOrderEntry.setFinstoreqty(param.getFgoodqty());

        xjfWorkOrderEntryService.updateById(xjfWorkOrderEntry);

        BeanUtil.copyProperties(newEntity, oldEntity);

        this.updateById(oldEntity);
        //redisTemplate.delete(redisTemplate.keys("getXjfWorkOrderEntrys::" + "*"));
    }

    @Override
    public XjfInStoreResult detail(Long fid) {
        XjfInStoreResult result = this.baseMapper.detail(fid);

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchChangeState(Object[] fid, WorkOrderEnum toEnum) {
        XjfInStore customer ;
        XjfWorkOrder xjfWorkOrder;
        System.out.print(fid.length);

        for(int i=0;i<fid.length;i++){
            customer = this.getById(Long.parseLong(fid[i].toString()));
            xjfWorkOrder = xjfWorkOrderService.getById(customer.getFworkorderid());

            if(customer.getFstatus()!=null && toEnum!=null && !customer.getFstatus().equals(toEnum)){

                if(toEnum.getCode()==1&&customer.getFstatus().getCode()==0) {
                    customer.setFstatus(toEnum);
                    XjfWorkOrderEntry xjfWorkOrderEntry = xjfWorkOrderEntryService.getById(customer.getFworkorderentrtyid());
                    xjfWorkOrderEntry.setFinstoreqty(xjfWorkOrderEntry.getFinstoreqty().add(customer.getFgoodqty()));

                    xjfWorkOrderEntryService.updateById(xjfWorkOrderEntry);
                    this.updateById(customer);
                }
                else if(toEnum.getCode()==1&&customer.getFstatus().getCode()!=0){
                    //审核异常操作
                    throw new ServiceException(500,"只有暂存状态下的订单才可变更为审核状态");
                }

                else if(toEnum.getCode()==0&&customer.getFstatus().getCode()==1)//审核状态下的反审核
                {
                    customer.setFstatus(toEnum);

                    XjfWorkOrderEntry xjfWorkOrderEntry = xjfWorkOrderEntryService.getById(customer.getFworkorderentrtyid());
                    xjfWorkOrderEntry.setFinstoreqty(xjfWorkOrderEntry.getFinstoreqty().subtract(customer.getFgoodqty()));
                    xjfWorkOrderEntryService.updateById(xjfWorkOrderEntry);

                    this.updateById(customer);
                }
            }
        }
    }

    private XjfInStore getOldEntity(XjfInStoreParam param)
    {
        return this.getById(getKey(param));
    }

    private Serializable getKey(XjfInStoreParam param){
        return param.getFid();
    }

    private Page getPageContext() {
        return PageFactory.defaultPage();
    }

}
